/**
 * 游戏视图
 */
(function(cjs,an){
    let p;
    /**
     * 视图基类
     */
    let BaseView=function(){
        this.Container_constructor();
        this.width=800;
        this.height=600;
        /**
         * 设置宽高
         * @param {Number} w 宽
         * @param {Number} h 高
         */
        this.setSize=function(w,h){
            //console.log('setSize')
            this.width=w;
            this.height=h;
            this.updateView();
        }
        /**
         * 更新界面
         */
        this.updateView=function(){

        }
    };
    p = cjs.extend(BaseView, cjs.Container);
    BaseView=cjs.promote(BaseView,'Container');
    /**
     * 预加载视图
     */
    let PreloadView=function(){
        this.BaseView_constructor();
        console.log('PreloadView');
        const {ProgressBar}=an.UIComponent;
        //背景
        let bg=new cjs.Shape();
        bg.parent=this;
        this.addChild(bg);
        //进度条
        let bar=new ProgressBar();
        bar.parent=this;
        this.addChild(bar);
        this.setProgress=function(value){
            bar.setProgress(value);
        };

        this.updateView=function(){
            bar.x=(this.width-bar.width)/2;
            bar.y=(this.height-bar.height)/2;

            bg.graphics.beginFill('#000000');
            bg.graphics.drawRect(0,0,this.width,this.height);
            bg.graphics.endFill();
        }
        this.updateView();
    };
    p = cjs.extend(PreloadView, BaseView);
    PreloadView=cjs.promote(PreloadView,'BaseView');
    /**
     * 开始游戏视图
     */
    let StartView=function(){
        this.BaseView_constructor();
        console.log('StartView');
        const {ViewEvent}=an
        const lib=an.lib;
        //背景
        const bg=new cjs.Shape();
        bg.parent=this;
        this.addChild(bg);
        //全屏按钮
        const bt_fullscreen=new lib.FullScreenButton();
        bt_fullscreen.y=10;
        bt_fullscreen.cursor="pointer";
        this.addChild(bt_fullscreen);
        bt_fullscreen.on('click',function(){
            //发送请求全屏事件
            this.dispatchEvent(ViewEvent.REQUEST_FULL_SCREEN,true);
        }.bind(this),null);
        //退出全屏按钮
        const bt_outfullscreen=new lib.OutFullScreenButton();
        bt_outfullscreen.y=bt_fullscreen.y;
        bt_outfullscreen.cursor="pointer";
        this.addChild(bt_outfullscreen);
        bt_outfullscreen.visible=false;
        bt_outfullscreen.on('click',function(){
            //发送请求退出全屏事件
            this.dispatchEvent(ViewEvent.REQUEST_OUT_FULL_SCREEN,true);
        },null);
        //穷字
        const poor=new lib.CachedTexturedBitmap_1();
        this.addChild(poor);
        //开始按钮
        const bt_start= new lib.StartButton();
        bt_start.cursor="pointer";
        this.addChild(bt_start);
        bt_start.on('click',function(){
            this.dispatchEvent(ViewEvent.START_GAME,true);
        },null);
        //
        Object.defineProperties(this, {fullScreen:{set:function(value){
            bt_outfullscreen.visible=value;
            bt_fullscreen.visible=!value;
        }}});

        this.updateView=function(){
            //console.log(bt_fullscreen.nominalBounds)
            bt_fullscreen.x=this.width-bt_fullscreen.nominalBounds.width-10;
            bt_outfullscreen.x=bt_fullscreen.x;
            //穷
            let scale=0.5;
            poor.setTransform((this.width-poor.getBounds().width*scale)/2,(this.height-poor.getBounds().height*scale)/2,scale,scale);
            //开始按钮
            bt_start.x=(this.width-bt_start.nominalBounds.width)/2;
            bt_start.y=this.height-bt_start.nominalBounds.height-20;
            //
            bg.graphics.beginFill('#000000');
            bg.graphics.drawRect(0,0,this.width,this.height);
            bg.graphics.endFill();
        };
        this.updateView();
    };
    p = cjs.extend(StartView, BaseView);
    StartView=cjs.promote(StartView,'BaseView');
    
    let GameingView=function(){
        this.BaseView_constructor();
        console.log('GameingView');
        const self=this;

        const fr = [] //富人
        const frq = [] //富人的钱
        const qions = [];
        const qians = []

        const {lib,DragHelper}=an;
        //背景
        const bg=new cjs.Shape();
        bg.parent=this;
        this.addChild(bg);
        //我
        const my_word=new lib.my();
        my_word.x=(this.width-my_word.nominalBounds.width)/2;
        my_word.y=(this.height-my_word.nominalBounds.height)/2;
        my_word.parent=this;
        this.addChild(my_word);
        DragHelper.addDragObject(my_word);

        this.updateView=function(){
            //
            bg.graphics.beginFill('#000000');
            bg.graphics.drawRect(0,0,this.width,this.height);
            bg.graphics.endFill();
        };
        this.updateView();
        //
        this.run=function(){
            this.on('tick',onTick,null);
        };
        let count=0;
        const onTick=()=>{
            if(count>=60){
                count=0;
                roundqion();
                roundqian();
                roundfr();
            }
            count++;

            qionMove();
	        hitqian();
	        frq_yd();
        };
        //随机出现穷
        function roundqion() {
            let r = Math.random()*10;
            if (r < 1) {
                return;
            }
            console.log("穷=" + r);
            let q = new lib.qion();
            q.x = Math.random() * canvas.width;
            q.y = Math.random() * canvas.height;
            self.addChild(q);
            qions.push(q);
            //console.log(ts.mc2.regX)
            setQionPot(q);
        }
        //钱出现的概率
	function roundqian() {
		let r = Math.random() * 10
		if (r > 1) {
			return
		}
		console.log("钱=" + r)
		let q = new lib.qian()
		q.x = Math.random() * 800
		q.y = Math.random() * 600
		self.addChild(q)
		qians.push(q)
		//重新设置穷的位置
		setQionPot()
	}
    //富人出现的概率
	function roundfr() {
		//只能同时存在一个
		if (fr.length != 0) {
			return
		}
		let r = Math.random() * 10
		if (r > 0.5) {
			return
		}
		let q = new lib.fu()
		q.x = Math.random() * 800
		q.y = Math.random() * 600
		console.log(q.y + "富=" + q.x)
		self.addChild(q)
		//当前在数组中的下表
		let len = fr.length
		fr[len] = q
		//撒钱次数
		let count = Math.floor(Math.random() * 10)
		//撒钱的间隔
		let jg = Math.random() * 10
		//每次撒出的数量
		let i = Math.floor(Math.random() * 20)
		let intval = 0
		//准备撒钱
		let itl = setInterval(function () {
			//设置撒钱对象
			AngelPots(i, q)
			intval++
			if (intval >= count) {
				clearInterval(itl);
				q.parent.removeChild(q)
				fr.splice(len, 1)
				console.log("剩下的富人=" + fr.length)
				//重新设置穷的位置
				setQionPot()
			}
		}, 1000 * jg)
		//重新设置穷的位置
		setQionPot()
	}
        function setQionPot(sel) {
            let tg
            let dis
            if (sel == undefined) {
                //设置所有的穷
                //改变所有穷的target，
                for (let i = 0; i < qions.length; i++) {
                    //先判断是否有富人，在判断是否有钱，最后跟随我
                    if (fr.length != 0) {
                        tg = new Vector(fr[0].x - fr[0].regX, fr[0].y - fr[0].regY);
                        dis = 50
    
                    } else if (qians.length != 0) {
                        tg = new Vector(qians[0].x - qians[0].regX, qians[0].y - qians[0].regY)
                        dis = 2
                    } else {
                        tg = new Vector(my_word.x - my_word.regX, my_word.y - my_word.regY)
                        dis = 30
                    }
                    qions[i].target = circlePot(dis, tg)
                }
                return
            }
    
            //先判断是否有富人，在判断是否有钱，最后跟随我
            if (fr.length != 0) {
                tg = new Vector(fr[0].x - fr[0].regX, fr[0].y - fr[0].regY)
                dis = 50
    
            } else if (qians.length != 0) {
                tg = new Vector(qians[0].x - qians[0].regX, qians[0].y - qians[0].regY)
                dis = 2
            } else {
                tg = new Vector(my_word.x - my_word.regX, my_word.y - my_word.regY)
                dis = 30
            }
            sel.target = circlePot(dis, tg)
        }
        //穷的移动
	function qionMove() {
		//console.log("没钱")
		qions.forEach(function (e) {
			e.moveTo(e.target, 2)
			//let pt = circlePot(30, ts.mc2)

		})
	}
    //与钱的碰撞
	function hitqian() {
		//我与自然产生的钱
		for (let i = 0; i < qians.length; i++) {
			//	console.log(Vector.distance(ts.mc2.getCenterPot(), qians[i].getCenterPot()))
			if (Vector.distance(ts.mc2.getCenterPot(), qians[i].getCenterPot()) <= 15) {
				qians[i].parent.removeChild(qians[i])
				qians.splice(i, 1)
				money++
				ts.txt.text = money
				setQionPot()
				break
			}
		}
		//我与富人产生的钱
		for (let i = 0; i < frq.length; i++) {
			if (Vector.distance(ts.mc2.getCenterPot(), frq[i].getCenterPot()) <= 15) {
				frq[i].parent.removeChild(frq[i])
				frq.splice(i, 1)
				money++
				ts.txt.text = money
				setQionPot()
				break
			}
		}
		//我与穷
		for (let i = 0; i < qions.length; i++) {
			if (Vector.distance(ts.mc2.getCenterPot(), qions[i].getCenterPot()) <= 15) {
				qions[i].parent.removeChild(qions[i])
				qions.splice(i, 1)
				money -= 5
				ts.txt.text = money
				setQionPot()
				break
			}
		}
		//穷与与自然产生的钱
		for (let i = 0; i < qions.length; i++) {
			for (let j = 0; j < qians.length; j++) {
				if (Vector.distance(qians[j].getCenterPot(), qions[i].getCenterPot()) <= 15) {
					qions[i].parent.removeChild(qions[i])
					qions.splice(i, 1)

					qians[j].parent.removeChild(qians[j])
					qians.splice(j, 1)
					setQionPot()
					break
				}
			}
		}
		//穷富人产生的钱
		for (let i = 0; i < qions.length; i++) {
			for (let j = 0; j < frq.length; j++) {
				if (Vector.distance(qions[i].getCenterPot(), frq[j].getCenterPot()) <= 15) {
					qions[i].parent.removeChild(qions[i])
					qions.splice(i, 1)

					frq[j].parent.removeChild(frq[j])
					frq.splice(j, 1)
					setQionPot()
					break
				}
			}
		}
	}
    };
    p = cjs.extend(GameingView, BaseView);
    GameingView=cjs.promote(GameingView,'BaseView');

    const circlePot = function (radius, v) {
        let angle = Math.random() * Math.PI * 2;
        let x = Math.cos(angle) * radius + v.x;
        let y = Math.sin(angle) * radius + v.y;
        return new Vector(x, y)
    }

    //return {PreloadView,StartView}
    an.PreloadView=PreloadView;
    an.StartView=StartView;
    an.GameingView=GameingView;
})(createjs = createjs||{}, AdobeAn = AdobeAn||{});
var createjs, AdobeAn;